package com.acme.bankapp.domain;

import java.io.Serializable;
import java.util.List;

import com.acme.bankapp.domain.accounting.Account;

/**
 * domain client
 */
public class Client implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8608160038093009419L;
	final private String name;
	final private Gender gender;

	private List<Account> accounts;	

	Client(String name, Gender gender){
		this.name = name;
		this.gender = gender;
	}

	void setAccounts(List<Account> accounts) {
		this.accounts = accounts;
	}
	
	/**
	 * Accounts list
	 * @return
	 */
	public List<Account> getAccounts(){
		return accounts;
	}
	
	void addAccount(Account account){
		accounts.add(account);
	}
	
	/**
	 * Get clients salutation string
	 * @return client salutation
	 */
	public String getClientSalutation(){
		return gender.getDesc()+ " "+name;
	}

	public Gender getGender() {
		return gender;
	}
	public String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Client other = (Client) obj;
		if (gender != other.gender) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}
	
	@Override
	public String toString() {
		String salutation = getClientSalutation()+"\n";
		StringBuilder builder = new StringBuilder();
		for(Account account : accounts){
			builder.append(account.toString());
		}
		return salutation+builder.toString();
	}
	
}
